<?php

use think\migration\Migrator;
use think\migration\db\Column;

/**
 * Class Node 授权/权限节点表
 */
class Node extends Migrator
{
    //生成数据的id
    public $id = 0;

    public function change()
    {
        $table = $this->table('node');

        $table->addColumn('router', 'string', ['limit' => 255, 'default' => '#', 'comment' => '前端路由路径']);
        $table->addColumn('icon', 'string', ['limit' => 255, 'default' => '#', 'comment' => '节点的icon,保留字段目前没实现']);
        $table->addColumn('url', 'string', ['limit' => 255, 'default' => '#', 'comment' => '后端节点路径']);
        $table->addColumn('title', 'string', ['limit' => 100, 'default' => '', 'comment' => '节点名称']);
        $table->addColumn('status', 'boolean', ['default' => 1, 'comment' => '节点状态(0:禁用,1:开启)']);
        $table->addColumn('allow', 'boolean', ['default' => 0, 'comment' => '是否是白名单节点(0:不是,1:是)']);
        $table->addColumn('show', 'boolean', ['default' => 1, 'comment' => '是否需要展示在前端页面上(0:不是,1:是)']);
        $table->addColumn('pid', 'integer', ['default' => 0, 'comment' => '父节点id']);
        $table->addColumn('create_time', 'integer', ['default' => 0]);
        $table->addColumn('update_time', 'integer', ['default' => 0]);
        /*$table->insert([
            [
                'id' => 1,
                'url' => '/system/login',
                'title' => '系统登陆',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
            [
                'id' => 2,
                'url' => '/system/logout',
                'title' => '系统退出',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
            [
                'id' => 3,
                'url' => '/system/menu',
                'title' => '系统侧边栏菜单',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
            [
                'id' => 4,
                'url' => '/system/menu/getNodeByRouter',
                'title' => '根据当前页面路由查找该节点的父节点',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
            [
                'id' => 5,
                'url' => '#',
                'title' => '系统管理',
                'create_time' => time()
            ],
            [
                'id' => 6,
                'router' => '/admin',
                'url' => '/system/admin',
                'title' => '管理员列表',
                'pid' => 5,
                'create_time' => time()
            ],
            [
                'id' => 7,
                'url' => '/system/admin/add',
                'title' => '系统管理员添加',
                'pid' => 6,
                'create_time' => time()
            ],
            [
                'id' => 8,
                'url' => '/system/admin/edit',
                'title' => '系统管理员编辑',
                'pid' => 6,
                'create_time' => time()
            ],
            [
                'id' => 9,
                'url' => '/system/admin/del',
                'title' => '系统管理员删除',
                'pid' => 6,
                'create_time' => time()
            ],
            [
                'id' => 10,
                'router' => '/role',
                'url' => '/system/role',
                'title' => '角色列表',
                'pid' => 5,
                'create_time' => time()
            ],
            [
                'id' => 11,
                'url' => '/system/role/add',
                'title' => '角色添加',
                'pid' => 5,
                'create_time' => time()
            ],
            [
                'id' => 12,
                'url' => '/system/role/edit',
                'title' => '角色编辑',
                'pid' => 5,
                'create_time' => time()
            ],
            [
                'id' => 13,
                'router' => '/role',
                'url' => '/system/role/del',
                'title' => '角色删除',
                'pid' => 5,
                'create_time' => time()
            ],


        ]);*/

        $table->insert($this->createTableData());

        $table->save();
    }

    /**
     * 生成迁移数据
     * @param string $model 模块
     * @param string[] $controllerArray 控制器
     * @return array
     */
    public function createTableData($model = 'system', $controllerArray = ['admin' => '管理员', 'role' => '角色', 'menu' => '菜单']){
        //获取白名单节点
        $allowTableData = $this->allowTableData();

        //系统管理 一级节点
        $systemId = $this->getId();
        $data = [
            [
                'id' => $systemId,
                'router' => "/",
                'url' => '/',
                'title' => '系统管理',
                'create_time' => time()
            ],
        ];

        //遍历控制器模块
        foreach ($controllerArray as $key => $value){
            $pid = $this->getId();

            $listItem = [
                'id' => $pid,
                'router' => "/$key",
                //'url' => "/$model/$key",
                'title' => $value,
                'pid' => $systemId,
                'create_time' => time()
            ];
            //菜单权限
           /* if($key === 'menu'){
                $listItem['allow'] = 1;
            }*/
            $data[] = $listItem;

            //列表
            $data[] = [
                'id' => $this->getId(),
                'url' => "/$model/$key",
                'title' => $value.'列表',
                'pid' => $pid,
                'create_time' => time()
            ];

            //每个控制器的操作
            foreach (['添加','编辑','删除','状态改变'] as $item){
                $option = '';
                if($item === '添加'){
                    $option = 'add';
                }
                if($item === '编辑'){
                    $option = 'edit';
                }
                if($item === '删除'){
                    $option = 'del';
                }
                if($item === '状态改变'){
                    $option = 'change_status';
                }
                $data[] = [
                    'id' => $this->getId(),
                    'url' => "/$model/$key/$option",
                    'title' => $value.$item,
                    'pid' => $pid,
                    'create_time' => time()
                ];
            }


            if($key === 'role'){
                //查询当前角色的授权菜单节点
                $data[] = [
                    'id' => $this->getId(),
                    'url' => "/system/role/getNodesByRoleId",
                    'title' =>'查询当前角色的授权菜单节点',
                    'pid' => $pid,
                    'create_time' => time()
                ];
                //角色权限节点编辑
                $data[] = [
                    'id' => $this->getId(),
                    'url' => "/system/role/role_authorize",
                    'title' =>'角色权限节点编辑',
                    'pid' => $pid,
                    'create_time' => time()
                ];
            }
        }
        return array_merge($allowTableData,$data);
    }

    /**
     * 生成固定的白名单数据
     * @return array
     */
    public function allowTableData(){
        $data = [
            [
                'id' => $this->getId(),
                'url' => '/system/login',
                'title' => '系统登陆',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
            [
                'id' => $this->getId(),
                'url' => '/system/logout',
                'title' => '系统退出',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],
           /* [
                'id' => $this->getId(),
                'url' => '/system/menu',
                'title' => '系统侧边栏菜单',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ],*/
            [
                'id' => $this->getId(),
                'url' => '/system/menu/getNodeByRouter',
                'title' => '根据当前页面路由查找该节点的父节点',
                'allow' => 1,
                'show' => 0,
                'create_time' => time()
            ]
        ];
        return $data;
    }

    public function getId(){
        return ++$this->id;
    }
}
